package com.un.ebs.sys.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.un.ebs.core.exception.BusinessException;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.sys.domain.Function;
import com.un.ebs.sys.dto.FunctionUpdateDto;
import com.un.ebs.sys.service.FunctionService;
import com.un.ebs.sys.service.ScriptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-09-15
 */
@RestController
@RequestMapping("/function")
@Api(value = "sys-fun")
public class FunctionController {

    @Resource
    private FunctionService service;

    @Resource
    private ScriptService scriptService;

    @PostMapping("save")
    @ApiOperation(value = "save", notes = "添加或更新")
    public WebResult save(@RequestBody FunctionUpdateDto req) {
        return WebUtils.success(service.save(req));
    }

    @PostMapping("updateContext")
    @ApiOperation(value = "更新函数内容", notes = "更新函数内容")
    public WebResult updateContext(@RequestParam String id, @RequestBody String context) {
        return WebUtils.success(service.updateFunContext(id, context));
    }

    @PostMapping("release")
    @ApiOperation(value = "发布")
    public WebResult release(@RequestParam String id, @RequestParam(required = false) String remark) {
        service.release(id, remark);
        return WebUtils.success();
    }

    @PostMapping("releaseCancel")
    @ApiOperation(value = "撤销发布")
    public WebResult releaseCancel(@RequestParam String id) {
        service.releaseCancel(id);
        return WebUtils.success();
    }

    @PostMapping("delete")
    @ApiOperation(value = "删除")
    public WebResult delete(@RequestParam String id) {
        service.delete(id);
        return WebUtils.success();
    }

    @PostMapping("renameTag")
    @ApiOperation(value = "删除")
    public WebResult renameTag(@RequestParam String oldName, @RequestParam String newName) {
        service.renameTag(oldName, newName);
        return WebUtils.success();
    }

    @PostMapping("list")
    @ApiOperation(value = "查询，所有函数", notes = "查询")
    public WebResult<IPage<Function>> query(@RequestParam(required = false) String searchText, @RequestParam(required = false) String tagName, @RequestParam int pageSize, @RequestParam int pageIndex) {
        return WebUtils.success(service.query(searchText, tagName, pageSize, pageIndex));
    }

    @PostMapping("listReleases")
    @ApiOperation(value = "查询，所有已发布的版本")
    public WebResult<List<Function>> queryReleases(@RequestParam(required = false) String tagName) {
        return WebUtils.success(service.queryReleases(tagName));
    }

    @PostMapping("listHistory")
    @ApiOperation(value = "查询，指定函数的所有历史版本")
    public WebResult<List<Function>> queryHistory(@RequestParam String funName) {
        return WebUtils.success(service.queryHistory(funName));
    }

    @PostMapping("listTagNames")
    @ApiOperation(value = "查询，所有类别标签", notes = "查询")
    public WebResult<List<String>> queryTagNames() {
        return WebUtils.success(service.queryTags());
    }

    @PostMapping("call/{funName}")
    @ApiOperation(value = "call", notes = "call")
    public WebResult<String> callFunction(@PathVariable String funName) {
        if (!scriptService.has(funName)) {
            return WebUtils.error(new BusinessException("无效的函数或函数尚未发布"));
        }
        try {
            return WebUtils.success(scriptService.call(funName, null));
        } catch (BusinessException ex) {
            System.out.println(ex.getMessage());
            return WebUtils.error(new BusinessException("函数调用发生错误"));
        }
    }

}